<?php

namespace App\Transformers\Admin;

use League\Fractal\TransformerAbstract;

class QuestionsTransformer extends TransformerAbstract
{
    public $availableIncludes = ['category', 'user'];

    public function transform($question)
    {
        return [
            'id' => $question->id,
            'user_id' => $question->user_id,
            'title' => $question->title,
            'content' => $question->content,
            'type' => $question->type,
            'category_id' => $question->category_id,
            'status' => $question->status,
            'fraction' => $question->fraction,
            'num' => $question->num,
            'answer' => $question->answer,
            'level' => $question->level,
            'success_answer' => $question->success_answer,
            'analysis' => $question->analysis,
            'created_at' => $question->created_at,
            'subsystem' => $question->subsystem ?? null
        ];
    }

    public function includeCategory($question)
    {
        return $this->item($question->category, new CategoryTransformer());
    }

    public function includeUser($question)
    {
        return $this->item($question->user, new class extends TransformerAbstract {
            public function transform($user) {
                return [
                    'id' => $user->id ?? null,
                    'username' => $user->username ?? null
                ];
            }
        });
    }
}

